package com.china08.yxyapi.api.manage.edu.office.innernotice;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner.YxyNotifictionTea;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 教育局行政办公 内部通知 搜索通知
 * 
 */
@CrossOrigin
@RestController("EduInnerNoticeReceiveSrhController")
public class InnerNoticeReceiveSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/edu/office/innerNotice/receive", method = RequestMethod.POST)
    public Page<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody ReqModel reqModel//
    ) {
        Pageable pageable = reqModel.getPageable(Direction.DESC, "createdDate");
        // 当前教育局id
        String schoolId = getProfile(request, response).getSchoolId();
        // 当前用户的获取
        String userId = getProfile(request, response).getId();
        // 未删除状态
        int isDelete = 0;

        List<YxyNoticeInner> page = yxyNoticeInnerRepository.findBySchoolIdAndReceiverUserIdAndReceiverIsDel(schoolId, userId, isDelete, pageable);
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        RespModel respModel = null;
        YxyNotifictionTea yxyNotifictionTea = null;
        for (YxyNoticeInner yxyNoticeInner : page) {
            respModel = new RespModel();
            respModel.id = (yxyNoticeInner.getId());
            respModel.title = (yxyNoticeInner.getTitle());
            respModel.userId = (yxyNoticeInner.getUserId());
            respModel.userNick = (yxyNoticeInner.getUserNick());
            respModel.schoolId = (yxyNoticeInner.getSchoolId());
            respModel.schoolNick = (yxyNoticeInner.getSchoolNick());
            if (yxyNoticeInner.getCreatedDate() != null) {
                respModel.createdDate = DateToolUtils.format(yxyNoticeInner.getCreatedDate(), "yyyy-MM-dd");
            }
            // JAVA 8 判断查询接受用户信息
            yxyNotifictionTea = yxyNoticeInner.getCurrentReceiver(userId);
            if (yxyNotifictionTea == null) {
                respModel.isRead = 0;
            } else {
                respModel.isRead = (yxyNotifictionTea.getIsRead());
            }
            // 获取发信人所在部门

            respModelLst.add(respModel);
        }
        int totalElements = yxyNoticeInnerRepository.countBySchoolIdAndReceiverUserIdAndReceiverIsDel(schoolId, userId, isDelete);
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, totalElements);
        return respModelPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel {
        // 消息id
        public String id;
        // 消息标题
        public String title;
        // 发件人id
        public String userId;
        // 发件人名称
        public String userNick;
        // 发布对象 教育局id
        public String schoolId;
        // 发布对象 教育局名称
        public String schoolNick;
        // 是否已读 0:未读 1:已读
        public int isRead;
        public String createdDate;
    }
}
